package work20241128;

import java.util.Arrays;

/**
 * @author zdl
 * @date 2024/11/28
 */
public class Demo8 {
    public static void main(String[] args) {
        //设计方法,在一个数组中，返回所有的 指定数据的下标
        //例如, 这个数组[1,2,8,4,5,7,8,7,8,9],找到其中元素8的下标[2,6,8]
        int[] arr = {1,2,8,4,5,7,8,7,8,9};
        int num = 8;
        int[] newArr = targetIndex(arr,num);
        System.out.println(Arrays.toString(newArr));

    }

    public static int[] targetIndex(int[] arr,int n){
        int[] tempArr = new int[arr.length];
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (n  == arr[i]){
                tempArr[count++] = i;
            }
        }
        int[] newArr = new int[count];
        for (int i = 0; i < newArr.length; i++) {
            newArr[i] = tempArr[i];
        }
        //System.out.println(Arrays.toString(newArr));
        return newArr;
    }
}
